package com.mars.push.core.im;

import com.mars.fw.common.utils.StringUtils;
import com.mars.fw.web.exception.KingException;
import com.mars.push.api.common.*;
import com.mars.push.api.dto.PushImMessageInput;
import com.mars.push.api.dto.PushMessageInput;
import com.mars.push.api.exception.PushException;
import com.mars.push.core.channel.PushChannelContext;
import com.mars.push.core.service.IPushService;
import com.google.api.client.util.Maps;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author King
 * @create 2020/5/25 10:59
 */
@Slf4j
@Service
public class ImAndroidPushService implements IPushService {

    @Autowired
    private PushChannelContext pushChannelContext;

    @Override
    public Object pushAssign(MessageInput input) {
        //TODO 业务消息实体重新包装
        try {
            PushImMessageInput imMessageInput = (PushImMessageInput) input;
            //todo 转成通道的业务实体
            PushMessageInput messageInput = transMessageInput(imMessageInput);
            return pushChannelContext.instance(PushType.J_PUSH.getCode()).androidPush(messageInput);
        } catch (PushException e) {
            throw new KingException(e.getMessage());
        } catch (Exception e) {
            throw new KingException("推送服务内部异常");
        }
    }

    @Override
    public Object push(MessageInput input) {
        //TODO 业务消息实体重新包装
        try {
            PushImMessageInput imMessageInput = (PushImMessageInput) input;
            //todo 转成通道的业务实体
            PushMessageInput messageInput = transMessageInput(imMessageInput);
            return pushChannelContext.instance(PushType.J_PUSH.getCode()).androidPush(messageInput);
        } catch (PushException e) {
            throw new KingException(e.getMessage());
        } catch (Exception e) {
            throw new KingException("推送服务内部异常");
        }
    }


    /**
     * IM推送实体转换成通用的业务实体
     *
     * @param imMessageInput
     * @return
     */
    private PushMessageInput transMessageInput(PushImMessageInput imMessageInput) {
        if (StringUtils.isBlank(imMessageInput.getDeviceToken())) {
            log.error("IM IOS推送的推送目标人为空{}", imMessageInput.toString());
            throw new KingException("IM android推送的推送目标人为空");
        }
        PushMessageInput messageInput = new PushMessageInput();
        messageInput.setNoticeType(NoticeType.REGISTER_ID)
                .setPushType(PushType.J_PUSH)
                .setPushMessageType(PushMessageType.NOTICE)
                .setDeviceType(DeviceType.ANDROID)
                .setBusinessType("IM")
                .setAlert(imMessageInput.getPushContent())
                .setTitle(imMessageInput.getTitle());
        messageInput.setTargets(Lists.newArrayList(imMessageInput.getDeviceToken()));

        Map<String, String> extras = Maps.newHashMap();
        extras.put("sender", imMessageInput.getSender());
        extras.put("senderName", imMessageInput.getSenderName());
        extras.put("target", imMessageInput.getTarget());
        extras.put("targetName", imMessageInput.getTargetName());

        messageInput.setExtras(extras);

        //todo 这边要处理的可能是PushContent 和 Title
        return messageInput;
    }


}
